home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1777 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.1 KB  |  47 lines

  1. Newsgroups: comp.lang.c++
  2. Path: hearst.acc.Virginia.EDU!maxwell!gcl8a
  3. From: gcl8a@Virginia.EDU (Gregory Carl Lewin)
  4. Subject: copy ctor and derived classes
  5. Message-ID: <DL38zz.50K@Virginia.EDU>
  6. Organization: University of Virginia
  7. Date: Fri, 12 Jan 1996 21:57:35 GMT
  8.  
  9. This may be a silly question, but I can't seem to find the
  10. answer in any of my reference books:
  11.  
  12. If I have a base class and a dervied class, and the derived
  13. class has a copy constructor, how can I invoke the base class's
  14. copy constructor in the intialization list so the derived class
  15. doesn't have to deal with all of the little bits of the base
  16. class?
  17.  
  18. e.g.
  19.  
  20. class Base{
  21.     int i;
  22.     public:
  23.         Base(const Base& b) : i(b.i) {}
  24. }
  25.  
  26. class Derived : public Base{
  27.     int j;
  28.     public:
  29.         Derived(const Derived& d) : /*tedious i(d.i) */
  30. j(d.j) {}
  31. };
  32.  
  33. assume there is a non-trivial reason to do this sort of thing
  34. (and that i didn't forget the ; at the end of Base :)
  35.  
  36. of course, this only works when i is protectted, right?  what
  37. if i is private (which it mistakenly is in my example)?
  38.  
  39. and what if the inheritance is virtual?  does your answer still
  40. hold?
  41.  
  42. please respond via email:
  43.  
  44. GCL8A@virginia.edu
  45.  
  46. Greg Lewin
  47.